package com.runa.monitor.platinterface.netservice.equipment;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.Meter;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.comm.entity.CommMeter;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.runa.RunaConcenRequestData;
import com.runa.rccprotocol.dto.tiangang.TianGangValveRequestData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 2024/9/25 9:53
 * zhangYB
 */
@Slf4j
public class RunaUnifyDriver extends Concentrator {

    private String model;

    public RunaUnifyDriver(String serial, Integer timeout, String electricityMeter, String model) {
        this(serial, timeout, electricityMeter);
        this.model = model;
    }

    public RunaUnifyDriver(String serial, Integer timeout, String electricityMeter) {
        super(serial, timeout, electricityMeter);
    }

    @Override
    public void readNodeEqu(List<Node> nodes, TaskFunction function, EquData.EquDataType type) {
        if ("RNTY(非杭能)".equals(model)) {
            log.debug("{} {}", serial(), 3);
            switch(type) {
                case METER:
                    readHzMeter(nodes, function);
                    return;
            }
        }
        for (Node node : nodes) {
            super.readNodeEqu(Collections.singletonList(node), function, type);
        }
    }

    public void readHzMeter(List<Node> nodes, TaskFunction function) {
        List<RunaConcenRequestData> requestList = createRequestList(nodes);
        for (RunaConcenRequestData requestData : requestList) {
            function.setStatus(requestData.getRemain());
            requestData.heatMeterData(requestData.getPns(), 202);
            log.debug("{} 汇中统一协议抄表: {}", serial(), ByteUtil.byteToHexString(requestData.getData()));
            sendRequest(requestData, function);
        }
    }

    public static void main(String[] args) {
        RunaUnifyDriver runaUnifyDriver = new RunaUnifyDriver("250114204", 1000, "");
        List<Node> nodes = new ArrayList<>();
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(49).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(63).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(71).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(79).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(87).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(95).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(103).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(111).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(119).build()));
        nodes.add(Meter.getMeter(CommMeter.builder().sequence(125).build()));
        List<RunaConcenRequestData> requestList = runaUnifyDriver.createRequestList(nodes);
        for (RunaConcenRequestData requestData : requestList) {
            requestData.heatMeterData(requestData.getPns(), 202);
            log.debug(" 汇中统一协议抄表: {}", ByteUtil.byteToHexString(requestData.getData()));
        }

    }


    public List<RunaConcenRequestData> createRequestList(List<Node> nodes) {
        List<Integer> pns = new ArrayList<>();
        List<Integer> remains = new ArrayList<>();
        List<RunaConcenRequestData> requests = new ArrayList<>();
        RunaConcenRequestData requestData;
        if (nodes.size() < 10 && !nodes.isEmpty()) {
            for (Node node : nodes) {
                pns.add(node.getIndex());
                remains.add(node.getIndex());
            }
            requestData = generateRunaRequestData(pns, remains);
            requests.add(requestData);
        } else {
            for (Node node : nodes) {
                pns.add(node.getIndex());
                remains.add(node.getIndex());
                if (pns.size() == 10) {
                    requestData = generateRunaRequestData(pns, remains);
                    requests.add(requestData);
                    pns = new ArrayList<>();
                    remains = new ArrayList<>();
                }
            }
            if (!pns.isEmpty()) {
                requestData = generateRunaRequestData(pns, remains);
                requests.add(requestData);
            }
        }
        return requests;
    }

    public RunaConcenRequestData generateRunaRequestData(List<Integer> pns, List<Integer> remains) {
        RunaConcenRequestData requestData = new RunaConcenRequestData(serial());
        requestData.setPns(pns);
        requestData.setRemain(remains);
        return requestData;
    }





}
